package logic

import (
	"context"
	"fmt"
	"module-name/models/mysql"
	"module-name/modulerpc/internal/svc"
	"module-name/modulerpc/modulerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *modulerpc.ProductAddRequest) (*modulerpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line
	var product = mysql.Product{
		Name:  in.Name,
		Price: float64(in.Price),
		Stock: int(in.Stock),
	}
	err := product.ProductAdd()
	if err != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &modulerpc.ProductAddResponse{
		ProId: int64(product.ID),
	}, nil
}
